﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Stack_Check
{
    class GamelogWatcher
    {
        private bool alive;

        private string path;

        public event FileSystemEventHandler DestinationChanged;

        private FileSystemWatcher watcher;

        public bool isAlive
        {
            get { return this.alive; }
        }

        public void Start()
        {
            try
            {
                watcher = new FileSystemWatcher(path, "*.txt");
                watcher.Created += watcher_Created;
                watcher.EnableRaisingEvents = true;
                alive = true;
            }
            catch(SystemException systemException)
            {
                throw;
            }
        }

        void watcher_Created(object sender, FileSystemEventArgs e)
        {
            if (DestinationChanged != null)
            {
                DestinationChanged(this, e);
            }

        }

        public void Abort()
        {
            if (alive)
                watcher.EnableRaisingEvents = false;
            alive = false;
        }

        public GamelogWatcher(string path)
        {
            this.path = path;
        }
    }
}
